//
//  MyHDSegViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/12/11.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "XLPageViewController.h"
//#import "HDEvaluateListViewController.h"
//#import "HDThumbListViewController.h"
#import "CommunityInteractionListViewController.h"

#import "MyHDSegViewController.h"

@interface MyHDSegViewController ()<XLPageViewControllerDelegate,XLPageViewControllerDataSrouce>

@property (nonatomic, strong) XLPageViewController *pageViewController;

@property (nonatomic, strong) UIButton *commentBtn;
@property (nonatomic, strong) UIButton *zanBtn;


@end

@implementation MyHDSegViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
}

- (void)initView {

    self.title = @"订单";
    self.view.backgroundColor = UIColorHex(0xffffff);
    
    self.navigationController.navigationBarHidden = YES;
    #if RT_INTERACTIVE_PUSH
        self.rt_navigationController.rt_enableInteractivePush = YES;
    #endif
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"public_back_black"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    
    [self initPageViewController];
    
    self.pageViewController.selectedIndex = 0;

    [self.view addSubview:btn];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kStatusBarHeight);
        make.left.mas_equalTo(0);
        make.height.mas_equalTo(NavBar_Height);
        make.width.mas_equalTo(60);
    }];
    
    [self.view addSubview:self.commentBtn];
    [self.view addSubview:self.zanBtn];
    
    [self.commentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(40);
        make.centerX.mas_equalTo(-30);
        make.top.mas_equalTo(5 + kStatusBarHeight);
    }];
    
    [self.zanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(40);
        make.centerX.mas_equalTo(30);
        make.top.mas_equalTo(5 + kStatusBarHeight);
    }];

    [RACObserve([TT_MessageManager manager], ZanCount) subscribeNext:^(id x) {
        if ([TT_MessageManager manager].ZanCount > 0) {
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                self.zanBtn.badgeValue = [NSString stringWithFormat:@"%d",[TT_MessageManager manager].ZanCount];
                self.zanBtn.badgeBGColor = [UIColor redColor];
            });

        } else {
            self.zanBtn.badgeValue = @"";
            self.zanBtn.badgeBGColor = [UIColor clearColor];
        }
    }];
    
    [RACObserve([TT_MessageManager manager], commentCount) subscribeNext:^(id x) {
        if ([TT_MessageManager manager].commentCount > 0) {
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                self.commentBtn.badgeValue = [NSString stringWithFormat:@"%d",[TT_MessageManager manager].commentCount];
                self.commentBtn.badgeBGColor = [UIColor redColor];
            });

        } else {
            self.commentBtn.badgeValue = @"";
            self.commentBtn.badgeBGColor = [UIColor clearColor];
        }
    }];
    
}

- (void)goBack {
    
    [[TT_TabbarViewController shareTabbar] setCurrentIndex:TT_TabbarIndexType_Community];
    [self.navigationController popToRootViewControllerAnimated:NO];
}

- (void)initPageViewController {
    
    XLPageViewControllerConfig *config = [XLPageViewControllerConfig defaultConfig];
    config.shadowLineColor = UIColorHex(0xFF4242);
    config.titleSelectedFont = [UIFont boldSystemFontOfSize:16];
    config.titleNormalFont = [UIFont systemFontOfSize:16];
    config.titleViewAlignment = XLPageTitleViewAlignmentCenter;
    config.showTitleInNavigationBar = false;
    config.separatorLineHidden = true;
    config.shadowLineAnimationType = XLPageShadowLineAnimationTypeZoom;
    config.shadowLineWidth = 20;
    config.titleWidth = 60;
    config.titleSpace = 0;
    self.pageViewController = [[XLPageViewController alloc] initWithConfig:config];

    self.pageViewController.delegate = self;
    self.pageViewController.dataSource = self;
    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];
    
    [self.pageViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kStatusBarHeight);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
//        make.height.mas_equalTo(NavBar_Height);
    }];
}

#pragma mark -
#pragma mark TableViewDelegate&DataSource
- (UIViewController *)pageViewController:(XLPageViewController *)pageViewController viewControllerForIndex:(NSInteger)index {
        
    CommunityInteractionListViewController *vc = [[CommunityInteractionListViewController alloc] initWithType:index];
    return vc;
}

- (NSString *)pageViewController:(XLPageViewController *)pageViewController titleForIndex:(NSInteger)index {
    return [self titles][index];
}

- (NSInteger)pageViewControllerNumberOfPage {
    return [self titles].count;
}

- (void)pageViewController:(XLPageViewController *)pageViewController didSelectedAtIndex:(NSInteger)index {
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [[TT_MessageManager manager] getInteractionCount:^{}];
    });
}

#pragma mark -
#pragma mark 标题数据
- (NSArray *)titles {
    return @[@"评论",@"赞"];
}

- (UIButton *)zanBtn {
    if (!_zanBtn) {
        _zanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _zanBtn.backgroundColor = UIColor.clearColor;
        _zanBtn.enabled = NO;
    }
    return _zanBtn;
}

- (UIButton *)commentBtn {
    if (!_commentBtn) {
        _commentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _commentBtn.backgroundColor = UIColor.clearColor;
        _commentBtn.enabled = NO;
    }
    return _commentBtn;
}

@end
